package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private  String app_id = "2021000117637114";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCjih4Tq7qcd1RBAOgpYsgvbEyDC4RUeWL3Lu/jxKRYcXOuBZbAZubAuqyvVxua2b845LCai3bdxQsAUt4oOEWR1mOBaVCK+DSPGj53OVxBc5byAdEamofHPj7V99VPh1/gYbeQzJo8X6MXQyudCT8P+kPf5hnR7XCfpBbDxOqSC0fg8oDaVTRLIKUtqDmHqyiiEFBd3UpVpu+UylR7DUNa21GDyUmP828HprD/3+rDfnfSB8gxNxItLj4xAmZcLG3WZXhffla34t3JvENzi8OaP2F4XcVP1ls4o5oAOrhqM2OYAHYTpcMfWvNAkPhegZdcTF2E72+rs8MkZV02pl05AgMBAAECggEARnCDK+3X3vT28Vi2przJhMOc4QTg5pGyMs2gvfZA42dacCYwHIc19OVy48MdPMF1P8gg2TAg7rykVGtdDO3P6VTyXxdBfdoSqWCgbzHhrr9e0LY/nrnYqdUBs5+f3TVt8nOkYVX/SOnYkXh5HJaS9wrQmWu+y1D+JYTD2WbDsktA2d8K1W2bhZV3nyQENVfZn14a4C893Utj+s5zQg8n06W+uJG4MwykXLXyd1/zES01N4NJp4CpSnhb4qV2Jbq7Go9ukK5SYCXxb2zxyYpHHzh3o5Vq6DdWhL8ZAJtA+QW5AimnD3ux/Y4AEkdyDIHW0Uk+Zw4NWu60BkoZobBe0QKBgQDcLCfgtBSPUz/w1oPZXltLElRU4oCke2Oo8LiAcqbrCw878z+secKm/i0lBi+RIAIJZqKmj1iVsrvClzFnaebafOpZ0c8GhmjkmCi58lqnnEex0XhbVY4t7uSMxN+/p5rOVfTZp1U6k4t1XGa4d9kZn3wNFINzvNnsMN6pmLJv/wKBgQC+JsWTqowXBwg8AdxDx/ubCc4+wM+yZjRRk4rA+7ex7KP6TOD1l96oZFyR1w9DLGz0vj60V/QsD+0AfS6wj6phDyokGkhe044OVIZboRPv+cNi7XrP+JfHK5YUhql+uZyEyipKGjuylCdyR8iOJhshK0YRrcv3f5eAD6g4re6yxwKBgQDFoVEOTzbhTD3p5fQmCp9x7HsMUuBcRHG5tJI4ojGYa636ByfRnt4N3bwSLtJFt3B8lcdMMQG4Jd6KFgL/q7zPxC7r/vuRBNdk0fxEpPmg2BQ21irt+QcNzhETFqktNLotmlSVhrFnSgd1ikKeNN4INPM6Hn68RflrHJbRr0IEawKBgA3I48PbtkIpGVi4Tc6AHeZUZKY59hSZwiszxfhNhziXzh9kGV7Ufqi4ddmAvLOC8fcgH42VXrcfBHBnos3b7F8S6Vwqp5pwPbkeOYtE9xwLgDWYv7F59Q7UQxID3R+5tvYNbdlnY3KDuQRY3dooOwlBiNfZQJMVpdb7y5coTMibAoGBAMrjdX/lmDXaAQLDntT1yCMZUfTVU6YCkjM6EGqorrcEF5SHBQ/o/UXTm8AgJlYf4OdAZ2Dqc2dHwPA56bcJjf7Dg0nbS6EnwoWC3b4QtPvpjTrSvhbT0Awtj+HJLsN2CrgIemYZh8SVVZeTVVj7A2pX1OuNJh6v/G0Q/Qs2IcEX";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7RajcvQxskQvpSo0wr4V9i6jYhL8TZowNEN83U8oOVhNE5rRAyPNoVi0YuU7NqozTpMV6r0565WJfLGHN0N3naD9gBORK5iL5OeD6oOOyYuQHd0hZ+j51T0saSYJKZJlHoia9nQ9Af3rEEw8gnTl5Y8/6huK1S0JXLu6derAdlCZ2Mv9BtuQB7FRKxdeRBvpiYIdhjLP9y3m367sNnXL2kNZ1DJ2uI/iH80tTGgJsQfYIMjSxzYvU7sBOoXMNHrC1XqXjgc8Vzz9d0rEPWkesPWUOc+Fx+/vXTghdep+pRvwVV1LVzGOEuUc3bFnIBncOhH/h+cFBZrgD6DYAI6WIQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 异步通知，支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url ="http://gkd2obobtp.52http.net/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，调用member服务的方法，展示所有订单列表信息
    private  String return_url ="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    // 支付页最大超时时间
    private String timeOut = "30m";


    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeOut+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，直接交给浏览器即可，只要浏览器显示这个页面，就会发送请求，自动来到支付宝的收银台页面
//        System.out.println("支付宝的响应："+result);

        return result;

    }
}
